/*
 * Tools.h
 *
 *  Created on: 09.07.2011
 *      Author: sili
 */

#ifndef CONTROLLER_H_
#define CONTROLLER_H_

#include "Tools.h"
#include <vdr/plugin.h>

#include "log4cxx/logger.h"
// #include "log4cxx/basicconfigurator.h"

#include "AALAction.h"

class AALItem;
class AALLocatorItem;
class AALSensorItem;
class AALActorItem;

class SystemFactory;
class TemplateFactory;
class PropertyFactory;
class VisFactory;
class AudioFactory;
class LogFactory;
class HttpServer;
class HttpClient;

#define HTTP_DOCUMENT_ROOT "html"

enum logMsgStatus{trace, debug, info, warn, error, fatal};

struct logMessage {
	long id;						// Message id
	char status[10];
	char text[MAX_LOG_MESSAGE_LEN];		// Message text (description of the message. e.g. Fileupload ok...)
	time_t timestamp;				// Message timestamp, UTC
};

struct actionResponse{
	enum messageType{ok, busy} type;
	string message;
};

class ActionListener{
private:
	const char * name;	// the internal actionListener name. Used only for logging.
						// So we know which class performs currently performs which Action.
public:
	ActionListener(const char * _name){
		this->name = strdup(_name);
	}

	const char * getName(){
		return this->name;
	}

	virtual void doAction(AALAction * _action);
	virtual void doMainSavedAction();
	virtual void discardAction(AALAction * _action);
};

	const char * logFormat(const char *fmt, ...);
	#define AAL_TRACE(logger, fmt, ...) LOG4CXX_TRACE(logger, logFormat(fmt, ## __VA_ARGS__))
	#define AAL_DEBUG(logger, fmt, ...) LOG4CXX_DEBUG(logger, logFormat(fmt, ## __VA_ARGS__))
	#define AAL_INFO(logger, fmt, ...) LOG4CXX_INFO(logger, logFormat(fmt, ## __VA_ARGS__))
	#define AAL_WARN(logger, fmt, ...) LOG4CXX_WARN(logger, logFormat(fmt, ## __VA_ARGS__))
	#define AAL_ERROR(logger, fmt, ...) LOG4CXX_ERROR(logger, logFormat(fmt, ## __VA_ARGS__))
	#define AAL_FATAL(logger, fmt, ...) LOG4CXX_FATAL(logger, logFormat(fmt, ## __VA_ARGS__))

class Controller {
private:
	static Controller * instance;

	SystemFactory * systemFactory;
	TemplateFactory * templateFactory;
	PropertyFactory * propFactory;
	VisFactory * visFactory;
	AudioFactory * audioFactory;
	LogFactory * logFactory;

	vector<ActionListener *> listeners;

	HttpServer * httpServer;
	HttpClient * httpClient;

public:

	static Controller * getInstance();

	Controller();
	virtual ~Controller();

	virtual void init(const char * _configDir);

	virtual void addListener(ActionListener * _listener);
	AALItem * getAnAALItem();
	virtual void publish(ActionType _actionType, const char * _message = NULL, AALItem * _aalItem = NULL);
	virtual void publish(AALAction * _action);
	virtual void discard(AALAction * _action);
	virtual void executeActionsInMainThread();

	virtual cOsdObject * mainMenuAction();

	virtual AALItem * getRootAALItem();
	virtual const char * getConfigDir();

	virtual TemplateFactory * getTemplateFactory();
	virtual PropertyFactory * getPropertyFactory();
	virtual LogFactory * getLogFactory();

	virtual AudioFactory * getAudioFactory();

	SystemFactory * getSystemFactory();

	AALItem * createAALItem(const char * _type, int _itemId);
};


#endif /* CONTROLLER_H_ */
